{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Perform String Shifts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: stringShift"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串的左右移"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个包含小写英文字母的字符串&nbsp;<code>s</code>&nbsp;以及一个矩阵&nbsp;<code>shift</code>，其中&nbsp;<code>shift[i] = [direction, amount]</code>：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>direction</code>&nbsp;可以为&nbsp;<code>0</code>&nbsp;（表示左移）或&nbsp;<code>1</code>&nbsp;（表示右移）。</li>\n",
    "\t<li><code>amount</code>&nbsp;表示&nbsp;<code>s</code>&nbsp;左右移的位数。</li>\n",
    "\t<li>左移 1 位表示移除&nbsp;<code>s</code>&nbsp;的第一个字符，并将该字符插入到 <code>s</code> 的结尾。</li>\n",
    "\t<li>类似地，右移 1 位表示移除&nbsp;<code>s</code>&nbsp;的最后一个字符，并将该字符插入到 <code>s</code> 的开头。</li>\n",
    "</ul>\n",
    "\n",
    "<p>对这个字符串进行所有操作后，返回最终结果。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = &quot;abc&quot;, shift = [[0,1],[1,2]]\n",
    "<strong>输出：</strong>&quot;cab&quot;\n",
    "<strong>解释：</strong>\n",
    "[0,1] 表示左移 1 位。 &quot;abc&quot; -&gt; &quot;bca&quot;\n",
    "[1,2] 表示右移 2 位。 &quot;bca&quot; -&gt; &quot;cab&quot;</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = &quot;abcdefg&quot;, shift = [[1,1],[1,1],[0,2],[1,3]]\n",
    "<strong>输出：</strong>&quot;efgabcd&quot;\n",
    "<strong>解释：</strong> \n",
    "[1,1] 表示右移 1 位。 &quot;abcdefg&quot; -&gt; &quot;gabcdef&quot;\n",
    "[1,1] 表示右移 1 位。 &quot;gabcdef&quot; -&gt; &quot;fgabcde&quot;\n",
    "[0,2] 表示左移 2 位。 &quot;fgabcde&quot; -&gt; &quot;abcdefg&quot;\n",
    "[1,3] 表示右移 3 位。 &quot;abcdefg&quot; -&gt; &quot;efgabcd&quot;</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>s</code>&nbsp;只包含小写英文字母</li>\n",
    "\t<li><code>1 &lt;= shift.length &lt;= 100</code></li>\n",
    "\t<li><code>shift[i].length == 2</code></li>\n",
    "\t<li><code>0 &lt;= shift[i][0] &lt;= 1</code></li>\n",
    "\t<li><code>0 &lt;= shift[i][1] &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [perform-string-shifts](https://leetcode.cn/problems/perform-string-shifts/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [perform-string-shifts](https://leetcode.cn/problems/perform-string-shifts/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abc\"\\n[[0,1],[1,2]]', '\"abcdefg\"\\n[[1,1],[1,1],[0,2],[1,3]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shifts: List[List[int]]) -> str:\n",
    "        for shift in shifts:\n",
    "            if shift[0] == 0:\n",
    "                for _ in range(shift[1]):\n",
    "                    s += s[0]\n",
    "                    s = s[1:]\n",
    "            elif shift[0] == 1:\n",
    "                for _ in range(shift[1]):\n",
    "                    s = s[-1] + s\n",
    "                    s = s[:-1]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        l = len(shift)\n",
    "        ls = len(s)\n",
    "        sft = 0\n",
    "        for i in range(l):\n",
    "            if shift[i][0] == 0:\n",
    "                sft -= shift[i][1]\n",
    "            else:\n",
    "                sft += shift[i][1]\n",
    "        if sft < 0:\n",
    "            sft = abs(sft)\n",
    "            sft %= ls\n",
    "            tmp = s[0:sft]\n",
    "            ans = s[sft:] + tmp\n",
    "        elif sft > 0:\n",
    "            sft %= ls\n",
    "            tmp = s[-sft:]\n",
    "            ans = tmp + s[0: - sft]\n",
    "        else:\n",
    "            ans = s\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        total = 0\n",
    "        for move in shift:\n",
    "            d, a = move\n",
    "            total -= a if d == 1 else -a\n",
    "        l = len(s)\n",
    "        total %= l\n",
    "        return s[total:] + s[:total]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        \"\"\" 输入：S\n",
    "            输出：新的S\n",
    "            需求：按照shift操作字符串。\n",
    "            基本思路：首先计算shift，\"\"\"\n",
    "        step=0\n",
    "        for i in shift:\n",
    "            if i[0]==0:\n",
    "                step+=i[1]\n",
    "            else:\n",
    "                step-=i[1]\n",
    "        step%=len(s)\n",
    "        s=s[step:]+s[:step]\n",
    "        return s\n",
    "            \n",
    "\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        move = 0\n",
    "        for d, a in shift:\n",
    "            move = move + a if d == 0 else move - a\n",
    "        move %= len(s)\n",
    "        return s[move:] + s[:move] if move != 0 else s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n = len(shift)\n",
    "        for i in range(n):\n",
    "            if shift[i][0]==0:\n",
    "                direct = 1\n",
    "                index = shift[i][1]%len(s)\n",
    "                \n",
    "            else:\n",
    "                direct = -1\n",
    "                index = len(s)-shift[i][1]%len(s)\n",
    "            s = s[index:] + s[0:index]\n",
    "\n",
    "        return s\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        m = 0\n",
    "        for (direc,step) in shift:\n",
    "            if direc == 0:\n",
    "                m += step\n",
    "            else:\n",
    "                m -= step\n",
    "        \n",
    "        \n",
    "        shift_steps = m % len(s)\n",
    "        return s[shift_steps:]+s[:shift_steps]\n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        s = list(s)\n",
    "        for x in shift:\n",
    "            if x[0] == 0:\n",
    "                for i in range(x[1]):\n",
    "                    _a=s.pop(0)\n",
    "                    s.append(_a)\n",
    "            if x[0] == 1:\n",
    "                for i in range(x[1]):\n",
    "                    _a=s.pop()\n",
    "                    s.insert(0, _a)\n",
    "        \n",
    "        return \"\".join(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n, target = len(s), 0\n",
    "        for sft in shift:\n",
    "            if sft[0] == 0:\n",
    "                target += sft[1]\n",
    "            else:\n",
    "                target -= sft[1]\n",
    "        target %= n\n",
    "        return s[target:] + s[:target]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        cum = functools.reduce(operator.__add__, [op[1] if op[0] else -op[1] for op in shift])\n",
    "        n = len(s)\n",
    "        cum %= n\n",
    "        return s if not cum else s[-cum:] + s[:-cum]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        #简单切片即可\n",
    "        s=list(s)\n",
    "        def move(cnt,direc):\n",
    "            nonlocal s\n",
    "            cnt%=n\n",
    "            if direc==0:\n",
    "                s=s[cnt:]+s[:cnt]\n",
    "            else:\n",
    "                cnt=n-cnt\n",
    "                s=s[cnt:]+s[:cnt]\n",
    "        n=len(s)\n",
    "        for direc,amount in shift:\n",
    "            move(amount,direc)\n",
    "        return \"\".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        \n",
    "        for dire, amt in shift:\n",
    "            amt %= len(s)\n",
    "            if dire == 0:\n",
    "                s = s[amt:] + s[:amt]\n",
    "            else:\n",
    "                s = s[-amt:] + s[:-amt]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        if True:\n",
    "            sum_shift = 0\n",
    "            for i, sub_shift in enumerate(shift):\n",
    "                sum_shift += sub_shift[1] if sub_shift[0]<1 else -sub_shift[1]\n",
    "            sum_shift = sum_shift%len(s)\n",
    "            return (s+s)[sum_shift:sum_shift+len(s)]\n",
    "\n",
    "\n",
    "\n",
    "        if False:\n",
    "            total_shift = 0\n",
    "            for sub_shift in shift:\n",
    "                if sub_shift[0]:\n",
    "                    total_shift+= sub_shift[1]\n",
    "                else:\n",
    "                    total_shift+= -sub_shift[1]\n",
    "            total_shift = total_shift%len(s)\n",
    "            if total_shift>=0:\n",
    "                s=s[-total_shift:] + s[:-total_shift]\n",
    "            else:\n",
    "                s = s[total_shift:] + s[:total_shift]\n",
    "            return s\n",
    "        else:\n",
    "            count  = sum((-1)**sh[0]*sh[1] for sh in shift)%len(s)\n",
    "            return s[count:]+s[:count]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        t = 0\n",
    "        for x, y in shift:\n",
    "            if x == 0:\n",
    "                t -= y\n",
    "            else:\n",
    "                t += y\n",
    "        if t == 0:\n",
    "            return s\n",
    "        elif t > 0:\n",
    "            t %= len(s)\n",
    "            return s[-t:] + s[:-t]\n",
    "        else:\n",
    "            t = abs(t)\n",
    "            t %= len(s)\n",
    "            return s[t:] + s[:t]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        for x in shift:\n",
    "            if x[0] == 0:\n",
    "                i += x[1]\n",
    "            elif x[0] == 1:\n",
    "                i -= x[1]\n",
    "            else:\n",
    "                pass\n",
    "        while i < 0:\n",
    "            i += n\n",
    "        while i >= n:\n",
    "            i -= n\n",
    "        return s[i:]+s[0:i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        s=list(s)\n",
    "        def move(cnt,direc):\n",
    "            nonlocal s\n",
    "            cnt%=n\n",
    "            if direc==0:\n",
    "                s=s[cnt:]+s[:cnt]\n",
    "            else:\n",
    "                cnt=n-cnt\n",
    "                s=s[cnt:]+s[:cnt]\n",
    "        n=len(s)\n",
    "        for direc,amount in shift:\n",
    "            move(amount,direc)\n",
    "        return \"\".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "\n",
    "        def l(n, s):\n",
    "            for i in range(n):\n",
    "                s = s[1:] + s[0]\n",
    "            return s\n",
    "        \n",
    "        def r(n, s):\n",
    "            for i in range(n):\n",
    "                s = s[-1] + s[:-1]\n",
    "            return s\n",
    "        \n",
    "        for i in shift:\n",
    "            if i[0] == 0:\n",
    "                s = l(i[1], s)\n",
    "            else:\n",
    "                s = r(i[1], s)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        for item in shift:\n",
    "            s = self.shift_func(s,item[0],item[1])\n",
    "        return s\n",
    "\n",
    "    def shift_func(self,s,direct,amount):\n",
    "        if amount>len(s):\n",
    "            amount = amount%len(s)\n",
    "        if direct == 0:\n",
    "            s = s[amount:]+s[:amount]\n",
    "        else:\n",
    "            s = s[-amount:]+s[:-amount]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        l = 0\n",
    "        r = 0\n",
    "        n = len(s)\n",
    "        for sh in shift:\n",
    "            if sh[0] == 0:\n",
    "                l += sh[1]\n",
    "            elif sh[0] == 1:\n",
    "                r += sh[1]\n",
    "        if l == r:\n",
    "            return s\n",
    "        elif l > r:\n",
    "            ll = (l - r) % n\n",
    "            # print(f'{s[:ll]}, {s[ll:]}')\n",
    "            return s[ll:] + s[:ll]\n",
    "        else:\n",
    "            rr = (r - l) % n\n",
    "            # print(f'{s[:-rr]}, {s[-rr:]}')\n",
    "            return s[-rr:] + s[:-rr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n = len(s)\n",
    "\n",
    "        for op, offset in shift:\n",
    "            offset %= n\n",
    "            if offset == 0:\n",
    "                continue\n",
    "            if op == 0:\n",
    "                s = s[offset:] + s[0:offset] \n",
    "            else:\n",
    "                s = s[-offset:] + s[0:n - offset] \n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        if True:\n",
    "            from itertools import accumulate\n",
    "            sum_shift = -sum([i[1] for i in shift if i[0]<1])\n",
    "            sum_shift += sum([i[1] for i in shift if i[0] ==1])\n",
    "            sum_shift = sum_shift%len(s)\n",
    "            print(sum_shift, s)\n",
    "            #return (s+s)[sum_shift:sum_shift+len(s)]\n",
    "            return s[-sum_shift:] + s[:-sum_shift]\n",
    "\n",
    "\n",
    "\n",
    "        if False:\n",
    "            total_shift = 0\n",
    "            for sub_shift in shift:\n",
    "                if sub_shift[0]:\n",
    "                    total_shift+= sub_shift[1]\n",
    "                else:\n",
    "                    total_shift+= -sub_shift[1]\n",
    "            total_shift = total_shift%len(s)\n",
    "            if total_shift>=0:\n",
    "                s=s[-total_shift:] + s[:-total_shift]\n",
    "            else:\n",
    "                s = s[total_shift:] + s[:total_shift]\n",
    "            return s\n",
    "        else:\n",
    "            count  = sum((-1)**sh[0]*sh[1] for sh in shift)%len(s)\n",
    "            return s[count:]+s[:count]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        for i in shift:\n",
    "            N = i[1]\n",
    "            n = N % len(s)\n",
    "            if i[0] == 0:\n",
    "                s_0 = s[n:]\n",
    "                s = s_0 + s[0:n] \n",
    "            if i[0] == 1:\n",
    "                s_1 = s[:-n]\n",
    "                s = s[-n:] + s_1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        for sh in shift:\n",
    "            amount = sh[1] % len(s)\n",
    "            if sh[0] == 1:\n",
    "                s = s[len(s)-amount:] + s[:len(s)-amount]\n",
    "            else:\n",
    "                s = s[amount:] + s[:amount]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        final_shift = 0\n",
    "\n",
    "        for direction, amount in shift:\n",
    "            final_shift += amount if direction == 0 else -amount\n",
    "        \n",
    "        final_shift %= len(s)\n",
    "\n",
    "        final_shift = final_shift if final_shift >= 0 else len(s) + final_shift\n",
    "        \n",
    "        return s[final_shift:] + s[:final_shift]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        result = 0\n",
    "        for d, a in shift:\n",
    "            if d == 0:\n",
    "                result -= a\n",
    "            else:\n",
    "                result += a\n",
    "\n",
    "        n = len(s)\n",
    "        if result >= 0:\n",
    "            result %= n\n",
    "            return s[-result:] + s[:-result]\n",
    "        else:\n",
    "            result = (-result)%n\n",
    "            return s[result:] + s[:result]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        t = 0\n",
    "        for x, y in shift:\n",
    "            if x == 0:\n",
    "                t -= y\n",
    "            else:\n",
    "                t += y\n",
    "        t %= len(s)\n",
    "        if t == 0:\n",
    "            return s\n",
    "        elif t > 0:\n",
    "            return s[-t:] + s[:-t]\n",
    "        else:\n",
    "            t = abs(t)\n",
    "            return s[t:] + s[:t]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        shift_sum = 0\n",
    "        for direction, amount in shift:\n",
    "            if direction == 0:\n",
    "                shift_sum -= amount\n",
    "            else:\n",
    "                shift_sum += amount\n",
    "\n",
    "        n = len(s)\n",
    "        shift_sum %= n\n",
    "        if shift_sum < 0:\n",
    "            shift_sum += n\n",
    "        elif shift_sum == 0:\n",
    "            return s\n",
    "        \n",
    "        return s[-shift_sum:] + s[:n - shift_sum]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n = len(s)\n",
    "        left_shift = 0\n",
    "        for direction, dist in shift:\n",
    "            if direction == 0:\n",
    "                left_shift += dist\n",
    "            else:\n",
    "                left_shift -= dist\n",
    "        left_shift %= n\n",
    "        if left_shift == 0:\n",
    "            return s\n",
    "        return s[left_shift: ] + s[ :left_shift]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        cur=0\n",
    "        for shi in shift:\n",
    "            if shi[0]==0:\n",
    "                cur+=shi[1]\n",
    "            else:\n",
    "                cur-=shi[1]\n",
    "        cur=cur%len(s)\n",
    "        if cur==0:\n",
    "            return s\n",
    "        if cur>0:\n",
    "            return s[cur:]+s[:cur]\n",
    "        else:\n",
    "            cur=-cur\n",
    "            return s[-cur:]+s[:len(s)-cur]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        pos = 0\n",
    "        for ix in range(len(shift)):\n",
    "            if shift[ix][0] == 0:\n",
    "                pos += shift[ix][1]\n",
    "            else:\n",
    "                pos -= shift[ix][1]\n",
    "        pos = pos%len(s)\n",
    "        return s[pos:]+s[:pos]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        count  = sum((-1)**sh[0]*sh[1] for sh in shift)%len(s)\n",
    "        return s[count:]+s[:count]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        class Link:\n",
    "            def __init__(self, val:str, pre=None, next=None):\n",
    "                self.val = val\n",
    "                self.pre = pre\n",
    "                self.next = next\n",
    "\n",
    "        if s == '':\n",
    "            return ''\n",
    "        lis = list(s)\n",
    "        head = Link(s[0])\n",
    "        pointer = head\n",
    "\n",
    "        if len(lis) == 1:\n",
    "            pointer.next = pointer\n",
    "            pointer.pre = pointer\n",
    "\n",
    "        for i in range(1, len(lis)):\n",
    "            new = Link(s[i])\n",
    "            pointer.next = new\n",
    "            new.pre = pointer\n",
    "            pointer = pointer.next\n",
    "            if i == len(lis)-1:\n",
    "                pointer.next = head\n",
    "                head.pre = pointer\n",
    "                pointer = pointer.next\n",
    "\n",
    "        for ele in shift:\n",
    "            if ele[0] == 0:\n",
    "                for _ in range(ele[1]):\n",
    "                    pointer = pointer.next\n",
    "            else:\n",
    "                for _ in range(ele[1]):\n",
    "                    pointer = pointer.pre\n",
    "        res = ''\n",
    "        for _ in range(len(lis)):\n",
    "            res += pointer.val\n",
    "            pointer = pointer.next\n",
    "        return res\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        k = 0\n",
    "        for i, j in shift:\n",
    "            if i == 1:\n",
    "                k -= j\n",
    "            else:\n",
    "                k += j\n",
    "        k %= len(s)\n",
    "        if k < 0:\n",
    "            return s[k:] + s[:k]\n",
    "        elif k > 0:\n",
    "            return s[k:] + s[:k]\n",
    "        else:\n",
    "            return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n = len(s)\n",
    "        d = int(sum((a - 0.5) * 2 * b for a, b in shift)) % n\n",
    "        return (s * 3)[n - d: 2 * n - d]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        move = 0\n",
    "        n = len(s)\n",
    "        for d, length in shift:\n",
    "            if d == 0:\n",
    "                move -= length\n",
    "            else:\n",
    "                move += length\n",
    "        move %= n\n",
    "        return s[n - move:] + s[:n - move]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        move_right_by = 0\n",
    "        for direction, amount in shift:\n",
    "            if direction == 0:\n",
    "                move_right_by -= amount\n",
    "            else:\n",
    "                move_right_by += amount\n",
    "        \n",
    "        print(move_right_by)\n",
    "        move_right_by %= len(s)\n",
    "\n",
    "        print(move_right_by)\n",
    "        print(\"s[-move_right_by:]\", s[-move_right_by:])\n",
    "        print(\"s[:-move_right_by]\", s[:-move_right_by])\n",
    "\n",
    "        return s[-move_right_by:] + s[:-move_right_by]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n = 0\n",
    "        for i, j in shift:\n",
    "            if i == 0:\n",
    "                n += j\n",
    "            else:\n",
    "                n -= j\n",
    "        n %= len(s)\n",
    "        return s[n:] + s[:n]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n = len(s)\n",
    "        right = 0\n",
    "        for shi in shift:\n",
    "            right=right+shi[1] if shi[0]>0 else right-shi[1]\n",
    "        right%=n\n",
    "        return s[-right:]+s[:-right]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        #直接遍历，每次遍历获取前置和后置的字符串\n",
    "        n = len(s)\n",
    "        for direction, amount in shift:\n",
    "            # print(direction, amount)\n",
    "            amount = amount % n \n",
    "            if direction == 1: \n",
    "                prefix, surfix = s[n-amount:], s[:n-amount]\n",
    "            else:\n",
    "                prefix, surfix = s[amount:],s[:amount]\n",
    "            \n",
    "            s = prefix + surfix \n",
    "            # print(s)\n",
    "        return s \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        #矩阵左右移动相似题\n",
    "     #左移是减、右移是加\n",
    "        mod=len(s)\n",
    "        for i in range(len(shift)):\n",
    "            if shift[i][0]==0:\n",
    "                mov_left=shift[i][1] % mod\n",
    "                s = s[mov_left:] + s[:mov_left]\n",
    "            else:\n",
    "                mov_right= shift[i][1] % mod\n",
    "                s = s[mod - mov_right:] + s[:mod - mov_right]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        def _shift_left(info):\n",
    "            return info[1:] + info[0]\n",
    "\n",
    "        def _shift_right(info):\n",
    "            return info[-1] + info[0: len(info) - 1]\n",
    "\n",
    "        for direction, amount in shift:\n",
    "            if direction == 0:\n",
    "                for i in range(amount):\n",
    "                    s = _shift_left(s)\n",
    "            else:\n",
    "                for i in range(amount):\n",
    "                    s = _shift_right(s)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        for command in shift:\n",
    "            if command[0] == 0:\n",
    "                for _ in range(command[1]):\n",
    "                    rest = s[1:]\n",
    "                    moveLetter = s[0]\n",
    "                    s = rest + moveLetter\n",
    "            else:\n",
    "                for _ in range(command[1]):\n",
    "                    rest = s[:-1]\n",
    "                    moveLetter = s[-1]\n",
    "                    s = moveLetter + rest\n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        n = len(s)\n",
    "\n",
    "        for d, v in shift:\n",
    "            v = v % n\n",
    "            if d == 0:\n",
    "                s = s[v:] + s[:v]\n",
    "            elif d == 1:\n",
    "                s = s[n-v:] + s[:n-v]\n",
    "\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        \n",
    "        offset = 0 # 右移位数\n",
    "        for dire, amt in shift:\n",
    "            if dire == 0:\n",
    "                offset += amt\n",
    "            else:\n",
    "                offset -= amt\n",
    "\n",
    "        offset %= len(s)  \n",
    "        if offset < 0: # 左移\n",
    "            offset += len(s)\n",
    "        return s[offset:] + s[:offset]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        for dire in shift:\n",
    "            if dire[1] >= len(s):\n",
    "                dire[1] = dire[1] % len(s)\n",
    "            if dire[0] == 0:\n",
    "                s =  s[dire[1]:] + s[0:dire[1]]\n",
    "            else:\n",
    "                s = s[len(s) - dire[1]:] +  s[0:len(s) - dire[1]]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        finalShift = 0\n",
    "        for sh in shift:\n",
    "            if sh[0] == 1:\n",
    "                finalShift -= sh[1]\n",
    "            else:\n",
    "                finalShift += sh[1]\n",
    "        \n",
    "        finalShift = finalShift % len(s)\n",
    "        return s[finalShift:] + s[:finalShift]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        result = 0\n",
    "        for d, a in shift:\n",
    "            if d == 0:\n",
    "                result -= a\n",
    "            else:\n",
    "                result += a\n",
    "\n",
    "        n = len(s)\n",
    "        if result >= 0:\n",
    "            result %= n\n",
    "        else:\n",
    "            result = 0 - (-result)%n\n",
    "            \n",
    "        return s[-result:] + s[:-result]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        for item in shift:\n",
    "            s = self.shift_func(s,item[0],item[1])\n",
    "        return s\n",
    "\n",
    "    \n",
    "    def shift_func(self,s,direct,amount):\n",
    "        if amount>len(s):\n",
    "            amount = amount%len(s)\n",
    "        if direct == 0:\n",
    "            s = s[amount:]+s[:amount]\n",
    "        else:\n",
    "            s = s[-amount:]+s[:-amount]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        result = 0\n",
    "        for d, a in shift:\n",
    "            if d == 0:\n",
    "                result -= a\n",
    "            else:\n",
    "                result += a\n",
    "\n",
    "        n = len(s)\n",
    "        if result >= 0:\n",
    "            result %= n\n",
    "            return s[-result:] + s[:-result]\n",
    "        else:\n",
    "            result = 0 - (-result)%n\n",
    "            \n",
    "        return s[-result:] + s[:-result]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def stringShift(self, s: str, shift: List[List[int]]) -> str:\n",
    "        left = 0\n",
    "        for ss in shift:\n",
    "            direction, amount = ss\n",
    "            if direction == 0:\n",
    "                left += amount\n",
    "            else:\n",
    "                left -= amount\n",
    "        \n",
    "        amount = left % len(s)\n",
    "\n",
    "        return s[amount:]+s[:amount]\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
